package com.platform.utils;

import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;

import java.util.Map;
import java.util.stream.Collector;
import java.util.stream.Collectors;

/**
 * 通用的键值对泛型类
 *
 * @param <S> key
 * @param <T> value
 * @author dennywang
 */
@ToString
@EqualsAndHashCode
@Data
public final class Pair<S, T> {

    private S key;
    private T value;

    public Pair() {

    }

    public Pair(S key, T value) {
        this.key = key;
        this.value = value;
    }

    public static <S, T> Pair<S, T> of(S key, T value) {
        return new Pair<>(key, value);
    }

    /**
     * 返回第一个元素key
     *
     * @return key
     */
    public S getKey() {
        return key;
    }

    /**
     * 返回第二个元素value
     *
     * @return value
     */
    public T getValue() {
        return value;
    }

    /**
     * 静态泛型方法，用来返回toMap
     *
     * @return
     */
    public static <S, T> Collector<Pair<S, T>, ?, Map<S, T>> toMap() {
        return Collectors.toMap(Pair::getKey, Pair::getValue);
    }
}
